# -*- coding: utf-8 -*-

from autoscrapy.db.mongo_db import mongoClient
from autoscrapy.utils import log
from autoscrapy.utils import basePlugin


LOG = log.get_logging(__name__)


class MongoLinkfilter(basePlugin.BasePlugin):
    DATABASE = 'autoscrapy'
    COLLECTION = 'links'

    def __init__(self, context):
        self._context = context
        self._client = mongoClient(self.DATABASE)

    def is_duplicate_url(self, url):
        query = self._client.find_entry_by_fields({
                'collection': self.COLLECTION,
                'link': url
        })
        if query.count():
            return True
        else:
            return False

    def add_url(self, url):
        if not self.is_duplicate_url(url):
            try:
                self._client.add_entrys([{
                    'collection': self.COLLECTION,
                    'link': url
                }])
            except Exception as e:
                LOG.error(" add url %s fail", url)
                LOG.exception(" add url occure exception %s", e)
    
    @classmethod
    def get_type(cls):
        return 'mongo'
